%Using Harberger formula to compare gains with and without intermediates
%Loads data on scale elasticities, initial shares, and reallocation terms
%for industrial policy under the two different assumptions.

%Then do comparison.

clear all;

K=32; %number of sectors
N=62; %includes ROW
K_man=15;    %number of manufacturing sectors

%Importing the data without intermediates
B=importdata('sim_params_no_int.csv'); %these are the estimated elasticities
gamma_base = [zeros(2,1);B(:,1);zeros(K-2-K_man,1)]; %Heterogeneous scale elasticities, non-manufacturing set to zero

VA_initial=importdata('va_initial.csv');    %These are the initial value added shares.  Row is country, column is sector

lab_real_opt_tp=importdata('lab_real_opt_tp.csv');   %This is the amount of labor reallocation when going from the trade policy only equilibrium to the optimum

%Importing the data WITH intermediates
C=importdata('sim_params_cap_int_go.csv'); %these are the estimated elasticities
gamma_int = [zeros(2,1);C(:,1);zeros(K-2-K_man,1)]; %Heterogeneous scale elasticities, non-manufacturing set to zero

GO_VA_initial=importdata('go_va_initial.csv');    %These are the initial value added shares.  Row is country, column is sector

GO_real_opt_tp=importdata('go_real_opt_tp.csv');   %This is the amount of gross input reallocation when going from the trade policy only equilibrium to the optimum


%Transposing matrices
VA_initial=VA_initial(:,2:K+1)';
lab_real_opt_tp=lab_real_opt_tp(:,2:K+1)';
GO_VA_initial=GO_VA_initial(:,2:K+1)';
GO_real_opt_tp=GO_real_opt_tp(:,2:K+1)';


%Harberger gains with and without intermediates
for i=1:N-1
    gains_base(i,1)=(1/2)*sum(VA_initial(:,i).*lab_real_opt_tp(:,i).*gamma_base);
    gains_int(i,1)=(1/2)*sum(GO_VA_initial(:,i).*GO_real_opt_tp(:,i).*gamma_int);
    gains_base_gova(i,1)=(1/2)*sum(GO_VA_initial(:,i).*lab_real_opt_tp(:,i).*gamma_base);
    gains_base_goreal(i,1)=(1/2)*sum(GO_VA_initial(:,i).*GO_real_opt_tp(:,i).*gamma_base);
    gains_base_vareal(i,1)=(1/2)*sum(VA_initial(:,i).*GO_real_opt_tp(:,i).*gamma_base);
    tot_real_base(i,1) = sum(VA_initial(:,i).*lab_real_opt_tp(:,i));
    tot_real_int(i,1) = sum(GO_VA_initial(:,i).*GO_real_opt_tp(:,i));
end

%Comparison
xxx=sum(gains_base_gova)/sum(gains_base)
zzz=sum(gains_int)/sum(gains_base)
qqq=sum(gains_base_goreal)/sum(gains_int)
ggg=sum(gains_base_vareal)/sum(gains_base)

bbb=median(abs(lab_real_opt_tp),"all")
bbb=median(abs(GO_real_opt_tp),"all")



